package com.macro.mall.component;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 定期删除日志的定时器
 * @author 高强
 * @Date 2020年11月24日
 */
@Component
public class DeleteLogTimingTask {

    /**
     * 每隔3天删一次catalina.out
     * @author 高强
     * @Date 2020年11月24日
     */
    @Scheduled(cron = "0 0 0 1/3 * *")
    private void deleteCaCatalina() {
        String pathAdminApi = "/usr/local/admin-api/apache-tomcat-7.0.105/logs/";
        String pathApi2 = "/usr/local/api2/apache-tomcat-7.0.105/logs";
        String fileName = "catalina.out";
        delAllFileForFileName(pathAdminApi,fileName);
        delAllFileForFileName(pathApi2,fileName);
    }

    /**
     * 每天清理一次日志，除了fileName
     * @author 高强
     * @Date 2020年11月24日
     */
    @Scheduled(cron = "0 0 0 1/1 * *")
    private void deleteExceptFileName() {
        String pathAdminApi = "/usr/local/admin-api/apache-tomcat-7.0.105/logs/";
        String pathApi2 = "/usr/local/api2/apache-tomcat-7.0.105/logs";
        String pathDebug = "/var/logs/spring.log/debug";
        String pathError = "/var/logs/spring.log/error";
        String fileName = "catalina.out";
        delAllFileExceptFileName(pathAdminApi,fileName);
        delAllFileExceptFileName(pathApi2,fileName);
        delAllFileExceptFileName(pathDebug,fileName);
        delAllFileExceptFileName(pathError,fileName);
    }

    /**
     * 删除指定文件夹下所有文件,除了fileName
     * @param path 文件所在绝对路径
     * @param fileName 不需要删除的文件名
     * @return
     */
    private boolean delAllFileExceptFileName(String path, String fileName) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (fileName.equals(tempList[i])) {
                continue;
            }
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            }else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
        }
        return true;
    }

    /**
     * 删除指定文件夹下的fileName
     * @param path 文件所在绝对路径
     * @param fileName 需要删除的文件名
     * @return
     */
    public static boolean delAllFileForFileName(String path, String fileName) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (fileName.equals(tempList[i])) {
                if (path.endsWith(File.separator)) {
                    temp = new File(path + tempList[i]);
                }else {
                    temp = new File(path + File.separator + tempList[i]);
                }
                if (temp.isFile()) {
                    temp.delete();
                }
                break;
            }
        }
        return true;
    }
}
